package org.gwtEss.table;

import com.google.gwt.event.dom.client.FocusEvent;
import com.google.gwt.event.dom.client.FocusHandler;
import com.google.gwt.user.client.ui.FocusWidget;
import com.google.gwt.user.client.ui.Focusable;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;


public abstract class SystemManagedColWidget<VO, W extends Widget> extends ColWidget<VO> implements HasDisplayText<VO>{
	public SystemManagedColWidget(String colName) {
		super(colName);
		// TODO Auto-generated constructor stub
	}
	
	/*public enum UserTriggerEvent {MOUSE_MOVE, MOUSE_CLICK};
	public abstract void resetWidget(VO data, W w);
	public abstract boolean resetData(W w, VO data);
	public abstract UserTriggerEvent flipWidgetEvent(); 
	public abstract TextDesc createDisplayText(VO data);
	public abstract W createColWidgetInstance();*/
	
	
	
	public SystemManagedColWidget(String colName, int suggestedWidth) {
		super(colName, suggestedWidth);
		// TODO Auto-generated constructor stub
	}

	public enum UserTriggerEvent {MOUSE_MOVE, MOUSE_CLICK};
	
	public abstract UserTriggerEvent flipWidgetEvent(); 

	public abstract W createColWidgetInstance();

	
	public abstract boolean resetData(W w, VO data);

	public abstract void resetWidget(VO data, W w);

	private W wInstance = null;
	
	private int width = -1;

	private VO lastData;  

	final void doResetData(VO data){
		if (wInstance == null)
			wInstance = createColWidgetInstance();
		resetData(wInstance, data);
	}
	
	final void doResetWidget(VO data){
		/*if (wInstance == null)
			wInstance = createColWidgetInstance();*/
		System.out.println("do reset widget");
		initWidget();
		resetWidget(data, wInstance);
		if (width != -1)
			((Widget)wInstance).setWidth(width + "px");
		if (wInstance instanceof Focusable)
			((Focusable)wInstance).setFocus(true);
//		return wInstance; 
	}
	
	final void changeWidgetWidth(int pxWidth){
		this.width = pxWidth;
		if (wInstance != null)
			((Widget)wInstance).setWidth(width + "px");
	}
/*	public boolean isWidgetCreated(){
		return (wInstance != null);
	}*/
	private final void initWidget(){
		if (wInstance != null) return;
		wInstance = createColWidgetInstance();	
	}
	final W getWidget(){
		initWidget();
		return wInstance;
	}
}
